package com.example.tests.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

import com.example.tests.logic.SortedListOf;
import com.example.tests.BrowserTest;
import com.example.tests.logic.User;

public class UserManagementPage extends MainTemplate {
	@FindBy(id = "username")
	private WebElement username;
	@FindBy(name = "password")
	private WebElement password;
	@FindBy(name = "password2")
	private WebElement password2;
	@FindBy(name = "submit")
	private WebElement submit;
	@FindBy(name = "email")
	private WebElement email;
	@FindBy(xpath = "//select ")
	private WebElement permission;
	@FindBy(xpath = "//img[ @alt=\"Remove\" ][ @title=\"Remove\" ]")
	private WebElement removeImg;
	@FindBy(xpath = "//div[ @class = 'content' ]/table/tbody/tr")
	public List<WebElement> userRows;
	private SortedListOf<User> cachedUsers;
	
	public UserManagementPage createUser(User userdata) {
		username.clear();
		username.sendKeys(userdata.getUsername());
		email.clear();
		email.sendKeys(userdata.getEmail());
		password.clear();
		password.sendKeys(userdata.getPassword());
		password2.clear();
		password2.sendKeys(userdata.getPassword());
		new Select(permission).selectByVisibleText(userdata.getRole());
		submit.click();
		cachedUsers = null;
		return MyPageFactory.getPage(driver,UserManagementPage.class);
	}
	  public UserManagementPage deleteUser(User user) {
		    for (WebElement row : userRows) {
		      if (user.equals(convertRowToUser(row))) {
		        row.findElement(By.xpath("./td[6]/a")).click();
		        wait_().until(alertIsPresent()).accept();
		        break;
		      }
		    }
		    cachedUsers = null;
		    return MyPageFactory.getPage(driver, UserManagementPage.class);
		  
	  }
	  public SortedListOf<User> getUsers() {
		    if (cachedUsers == null) {
		      cachedUsers = new SortedListOf<User>();
		      for (WebElement row : userRows) {
		        cachedUsers.add(convertRowToUser(row));
		      }
		    }
		    return new SortedListOf<User>(cachedUsers);
		  }
		  
		  public int getUserCount() {
		    return userRows.size();
		  }

		  private User convertRowToUser(WebElement row) {
		    List<WebElement> cells = row.findElements(By.tagName("td"));
		    return new User()
		      .withUsername(cells.get(1).getText())
		      .withEmail(cells.get(2).getText())
		      .withRole(cells.get(3).getText());
		  }  
	public boolean isUserRemoved(User user) {
		boolean result = isAbleToLogin(user);
		MyPageFactory.getPage(driver, LoginPage.class )
			.login(BrowserTest.admin)
			.goUserManagementPage();
		return result;
	}

	public boolean isAbleToLogin(User user) {
		boolean result;
		this.logout();
		result =  MyPageFactory.getPage(driver, LoginPage.class)		
					.login(user)
					.IsLoginSuccessfull();
		this.logout();
		return result;
	}
	@Override
	public boolean isOnThisPage() {
		return (1 < driver.findElements( By.className("content") ).size() ) 
				&& 0 < driver.findElements(By.name("email")).size();
	}

	@Override
	void tryToOpen() {
		MyPageFactory.getPage(driver, LoginPage.class)
				.loginSuccesfullAs("admin", "admin")
				.goUserManagementPage();
	}
}
